package com.jmga.ecogasfinder.activities.maps.overlays;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Projection;

public class FinderLocationOverlay extends MyLocationOverlay {

	private boolean showCoveredArea;
	private GeoPoint lastLocationKnown;
	private float metersPerimeter = 0;

	public FinderLocationOverlay(Context context, MapView mapView, boolean showCoveredArea) {
		super(context, mapView);
		this.showCoveredArea = showCoveredArea;
	}

	@Override
	public synchronized boolean draw(Canvas canvas, MapView mapView,
			boolean shadow, long when) {
		Projection projection = mapView.getProjection();
		int mRadius = 5;
		Paint paint = new Paint();
		
		if(showCoveredArea) {
			paint.setAntiAlias(true);
			paint.setStrokeWidth(0.5f);
			paint.setStyle(Paint.Style.STROKE);
			paint.setColor(Color.BLACK);
			
			GeoPoint myLoc = this.getMyLocation();
			if(myLoc != null) {
				Log.d(getClass().getName(), "FinderLocationOverlay-Location (" + myLoc.getLongitudeE6() + "," + myLoc.getLatitudeE6() + ")");
				lastLocationKnown = myLoc;
			}
			
			if(lastLocationKnown != null) {
				Point centerPoint = new Point();
				projection.toPixels(lastLocationKnown, centerPoint);

				int northY = centerPoint.y - (int) projection.metersToEquatorPixels(this.metersPerimeter );
				int southY = centerPoint.y + (int) projection.metersToEquatorPixels(this.metersPerimeter );

				int westX = centerPoint.x - (int) projection.metersToEquatorPixels(this.metersPerimeter );
				int eastX = centerPoint.x + (int) projection.metersToEquatorPixels(this.metersPerimeter );
				
				canvas.drawRect((float) westX, (float) northY, (float) eastX , (float) southY, paint);
			}

		}
		
		paint.setARGB(250, 153, 0, 0);
		paint.setAntiAlias(true);
		paint.setFakeBoldText(true);
		
		if(lastLocationKnown != null) {
			// Convert the location to screen pixels
			Point point = new Point();
			projection.toPixels(lastLocationKnown, point);
			
			RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
					point.x + mRadius, point.y + mRadius);
	
			// Draw the marker
			canvas.drawOval(oval, paint);
		}
		
		return super.draw(canvas, mapView, shadow, when);
	}
	
	public GeoPoint getLastLocationKnown() {
		return this.lastLocationKnown;
	}

	public void setLastLocationKnown(Location location) {
		Double geoLat = location.getLatitude() * 1E6;
		Double geoLng = location.getLongitude() * 1E6;
		this.lastLocationKnown = new GeoPoint(geoLat.intValue(), geoLng.intValue());
	}

	public void setMetersPerimeter(float metersPerimeter) {
		this.metersPerimeter = metersPerimeter;
	}
}
